package com.mechat.ui

import UserUtil
import android.content.Intent
import android.location.Address
import android.location.Geocoder
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import androidx.constraintlayout.widget.ConstraintLayout
import com.custom.base.entity.OkHttpBodyEntity
import com.custom.base.http.OkHttpFromBoy
import com.custom.base.http.OkHttpManager
import com.custom.base.http.OkHttpResoutCallBack
import com.custom.base.http.SDOkHttpResoutCallBack
import com.custom.base.manager.SDFragmentManager
import com.custom.base.mvp.MVPBaseActivity
import com.custom.base.util.IntentUtil
import com.custom.base.util.ToastUtil
import com.google.android.gms.tasks.OnCompleteListener
import com.google.firebase.messaging.FirebaseMessaging
import com.mechat.Constant
import com.mechat.R
import com.mechat.entity.BaseEntity
import com.mechat.ui.chat.profile.UserProfileInfoActivity
import com.sunday.eventbus.SDBaseEvent
import io.rong.imkit.event.EnumEventTag
import kotlinx.android.synthetic.main.act_home.*
import java.util.Locale


/**
 * 作者：
 * 时间：
 * 描述：主页
 */

class HomeActivity : MVPBaseActivity<HomeContract.View, HomePresenter>(), HomeContract.View {
    override fun bindLayout(): Int {
        return R.layout.act_home
    }


    override fun initView() {
        //获取FCM的push token
        FirebaseMessaging.getInstance().token.addOnCompleteListener(OnCompleteListener { task ->
            if (!task.isSuccessful) {
                return@OnCompleteListener
            }

            val token = task.result
            OkHttpManager.instance.requestInterface(object : OkHttpFromBoy {
                override fun addBody(requestBody: OkHttpBodyEntity) {
                    requestBody.setPost(Constant.user_push_token_url)
                    requestBody.add("sourceType",1)
                    requestBody.add("token",token)
                }

            },object : SDOkHttpResoutCallBack<BaseEntity>(false) {
                override fun onSuccess(entity: BaseEntity) {

                }

            })
        })

        OkHttpManager.instance.setHttpCodeListener(object : OkHttpResoutCallBack.OnHttpCode {
            override fun code(code: Int, msg: String, url: String) {
                when (code) {
                    700 -> {
                        Toast.makeText(mActivity,mActivity.getString(R.string.account_logout_tip),Toast.LENGTH_SHORT).show()
                        UserUtil.out(mActivity)
                    }
                }
            }
        })

//        RouteUtils.registerActivity(RouteUtils.RongActivityType.ConversationListActivity, HomeActivity::class.java)
    }

    override fun setFullScreen(): Boolean {
        return true
    }

    override fun onEventMainThread(event: SDBaseEvent) {
        when (EnumEventTag.valueOf(event.tagInt)) {
            EnumEventTag.GO_USER_PROFILE -> {
                val userCode = event.data.toString()
                val bundle = Bundle()
                bundle.putString("userCodeFriend",userCode)
                IntentUtil.startActivity(UserProfileInfoActivity::class.java, bundle)
            }
            EnumEventTag.REFRESH_CHAT_SIZE -> {
                if(event.data.toString().toInt()>0){
                    txtImNoRead.visibility= View.VISIBLE
                    txtImNoRead.text=if(event.data.toString().toInt() > 99) "99+" else event.data.toString()
                }else{
                    txtImNoRead.visibility= View.GONE
                }
            }

            else -> {

            }
        }

    }

    override fun onSaveInstanceState(outState: Bundle) {
        mPresenter?.onSaveInstanceState(outState,this)
        super.onSaveInstanceState(outState)
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mPresenter?.onCreate(savedInstanceState,this)
    }

    private var mExitTime: Long = 0
    override fun onBackPressed() {
        if (System.currentTimeMillis() - mExitTime > 2000) {
            ToastUtil.toast("Press exit again!")
        } else {
            Runtime.getRuntime().exit(0)
        }
        mExitTime = System.currentTimeMillis()
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        mPresenter?.onActivityResult(requestCode, resultCode, data)

    }

    override val fragmentId: FrameLayout
        get() = home_frame
    override val tabs: List<ConstraintLayout>
        get() =  listOf(act_home_tab1,act_home_tab2)
    override val tabIvs: List<ImageView>
        get() =  listOf(tx_home_tab1,tx_home_tab2)
    override val tabTxts: List<TextView>
        get() = listOf(txt_home_tab1,txt_home_tab2)
    override val fm: SDFragmentManager
        get() = getSDFragmentManager()
    override val bottomContainer: LinearLayout
        get() = bottom_container
    override val txtImNoRead: TextView
        get() = txt_im_no_read

}